<?php
    /*
     * @Author: KingMin 
     * @Date: 2022-3-12 11:38:01 
     * @Last Modified by:   KingMin 
     * @Last Modified time: 2022-3-12 11:38:08
     * @The authors website: https://www.kingmin.cn  
     */
    header('Access-Control-Allow-Headers:x-requested-with,content-type'); 
    
    require_once ("./webapp.php");
    $APIURL=$_SERVER['REQUEST_URI'];
    $APIURL=explode("?",$APIURL);
    $APIURL=explode("/",$APIURL[0]);
    @$a=$APIURL[3];
    @$postData=json_decode(file_get_contents('php://input'), true);
    use GatewayClient\Gateway;
    switch ($a){
        case 'test':
            $ts=date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
            // $beginWeek = mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"));
            $beginWeek = date('Y-m-d', strtotime('-2 monday', time()));
            $yearM=31536000;
            // $data=date('Y-m-d', (time()-($yearM*22)));
            // $data=Option::getGroup('web_navi');
            // Gateway::$registerAddress = '127.0.0.1:1238';
            // $nv=getnavi();
            // $list=$nv['list'];
            // $list[1]['badge']=1;
            // $data= Gateway::sendToUid(4, json_encode(array('type' => 'naviList', 'message' => '测试','data'=>$list)));
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
            $thumbnail="https://kmlove.oss-cn-chengdu.aliyuncs.com/goeasy-im-kmlove/uni-video_47461_202209231551.mp4?x-oss-process=video/snapshot,t_0000,f_jpg,w_720,m_fast,ar_auto";
            $imgdetail=getimagesize($thumbnail);
            ajaxReturn (200,'操作成功',$imgdetail); 
        break;
        case 'wappconfig':
            @$path=$_GET['pagePath']??"";
            ajaxReturn (200,'操作成功',getwappconfig($path)); 
        break;
        case 'user'://用户模块
            @$userType=$APIURL[4];
             switch ($userType){
                case 'sendSmsCode':
                    $data=phoneLoginSend($postData);
                    if($data){
                        ajaxReturn (200,'操作成功',$data); 
                    }else{
                        ajaxReturn (500,'操作失败',$data); 
                    }
                break;
                case 'register':
                    $data=userRegister($postData);
                    if($data){
                        ajaxReturn (200,'操作成功',$data); 
                    }else{
                        ajaxReturn (500,'操作失败',$data); 
                    }
                break;
                case 'signIn':
                    $data=signIn($postData);
                    if($data){
                        ajaxReturn (200,'操作成功',$data); 
                    }else{
                        ajaxReturn (500,'操作失败',$data); 
                    }
                break;
                case 'userDetail':
                    @$data=userDetail($APIURL[5]);
                    if($data){
                        ajaxReturn (200,'操作成功',$data); 
                    }else{
                        ajaxReturn (500,'操作失败',$data); 
                    }
                break;
                case 'isLogin':
                    ajaxReturn (200,'操作成功',isLogin(false)); 
                break;
                case 'logOut':
                    logOut(); 
                break;
                default:
                    ajaxReturn(300,'FAIL',$a);
            }
        break;
        case 'wechat'://微信模块
            @$wechatType=$APIURL[4];
             switch ($wechatType){
                case 'config':
                    // $data=phoneLoginSend($postData);
                    @$url=$_GET['url'];
                    $data=[
                        "appId"=>"wx5fa917d0a5395ede",
                        "url"=>$url,
                    ];
                    if($data){
                        ajaxReturn (200,'操作成功',$data); 
                    }else{
                        ajaxReturn (500,'操作失败',$data); 
                    }
                break;
                default:
                    ajaxReturn(300,'FAIL',$a);
            }
        break;
        case 'chat'://聊天模块
            @$chatType=$APIURL[4];
            switch ($chatType){
                case 'bind':
                    
                break;
                case 'chatList':
                    $data=getChatList($postData);
                    if($data){
                        ajaxReturn (200,'操作成功',$data); 
                    }else{
                        ajaxReturn (500,'操作失败',$data); 
                    }
                break;
                case 'chatDetailsList':
                    $data=getChatDetailsList($postData);
                    if($data){
                        ajaxReturn (200,'操作成功',$data); 
                    }else{
                        ajaxReturn (500,'操作失败',$data); 
                    }
                break;
                case 'sendMessage':
                    $data=sendMessage($postData);
                    if($data){
                        ajaxReturn (200,'操作成功',$data); 
                    }else{
                        ajaxReturn (500,'操作失败',$data); 
                    }
                break;
                case 'upChatDetails':
                    $data=upChatDetails($postData);
                    if($data){
                        ajaxReturn (200,'操作成功',$data); 
                    }else{
                        ajaxReturn (500,'操作失败',$data); 
                    }
                break;
                case 'upChatList':
                    $data=upChatList($postData);
                    if($data){
                        ajaxReturn (200,'操作成功',$data); 
                    }else{
                        ajaxReturn (500,'操作失败',$data); 
                    }
                break;
                default:
                 ajaxReturn (1001,'没有此模块', $APIURL); 
                break;
            }
        break;
        default:
            // echo json_encode($APIURL);
            // header("HTTP/1.1 404 Not Found");
            // header("HTTP/1.1 404 Not Found");exit;
            // http_response_code(401);
            // exit(json_encode(2004, JSON_UNESCAPED_UNICODE));
            // return;
            // header("Location://admin.kingmin.cn/404");exit;
            ajaxReturn (1001,'非正常调用,验证不通过', $APIURL); 
    }